package wechat.rcv.controller

import edu.fudan.mylang.pf.IObjectFactory
import java.text.SimpleDateFormat
import org.apache.commons.collections.IteratorUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController
import org.apache.log4j.Logger
import com.shrcl.engineerprocess.domain.EngineerProjectAssignmentStatus
import com.shrcl.engineerprocess.domain.EngineerWorkOrder
import java.util.Collection
import com.shrcl.engineerprocess.domain.EngineerProjectAssignment
import java.util.ArrayList
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.ui.Model

@Controller
class AssignmentList {
	final static Logger logger = Logger.getLogger(typeof(AssignmentList).getName());


	@RequestMapping("/assignmentlist")
	def String process(
		@RequestParam(value="workorderId", defaultValue="") Long workorderId,
		Model model
	) {
		logger.info('=========>AssignmentList.processs<============')
		var hpage = 'error'
		logger.info('=========>AssignmentList.workorderId<============'+workorderId)

		
		model.addAttribute("workorderId",workorderId);
		
		hpage = "myassignment"
	}
	
}



@RestController
@Transactional
@ResponseBody
@RequestMapping("/myassignment")
class GetMyAssignment {
	final static Logger logger = Logger.getLogger(typeof(GetMyAssignment).getName());

	@Autowired IObjectFactory entities;
//	@Autowired CorpWeChatUserDAO corpWeChatUserDAO

	@RequestMapping(method = RequestMethod::POST,  produces = "application/json;charset=UTF-8" )
	def ArrayWapper process(Long workorderId, Integer page, Integer pagesize) {
		logger.info('=========>GetMyAssignment.processs<============')
		logger.info('=========>GetMyAssignment.wechatid<============'+workorderId)
		logger.info('=========>GetMyAssignment.page<============'+page)
		logger.info('=========>GetMyAssignment.pagesize<============'+pagesize)
		try{
			val e = entities.get(typeof(EngineerWorkOrder), workorderId).getProjectAssignments
//			val c = (entities.createQueryCriteria(typeof(EngineerProjectAssignmentCriteria))=>[
//				it.^start = (page - 1) * pagesize
//				it.^limit = pagesize 
//			])
			var ^start = (page - 1) * pagesize
			var ^end = page * pagesize 

			val ret = new ArrayWapper
			val Collection<EngineerProjectAssignment> e_part = new ArrayList
			for (var i = ^start; i<^end && i<e.size; i++) {
				e_part.add(e.get(i) as EngineerProjectAssignment)
			}
			val result = e_part.^map[a|
				new WXNotification=>[
					it.id = a.getId
					it.title = a.getWorkOrder.getRCProjectName
					it.date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getStartDate)+' to ' + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getEndDate)
					println('=========>GetMyAssignment.Status<============'+a.getStatus)
					if (a.getStatus == EngineerProjectAssignmentStatus.executing) {
						it.isFinished = false
					} else it.isFinished = true
				]
			]
			ret.result = IteratorUtils::toList(result.iterator);     
			ret.count = ret.result.size
			println("*******GetMyAssignment.result.size******"+ret.count)
			println("*******GetMyAssignment.response******")
			return ret
		}catch(Exception e){
			e.printStackTrace
			null
		}
	}
}